package cn.gson.springboot.model.service.cusservice;

import cn.gson.springboot.model.mapper.cusmapper.CuViewMapper;
import cn.gson.springboot.model.vo.cusvo.CuSelectOrderNum;
import cn.gson.springboot.model.vo.cusvo.CuViewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CuViewService {
    @Autowired
    CuViewMapper cuViewMapper;//统计图mapper

    //    客户创建时间的月份
    public List<String> createMonth(Integer empId){
        return cuViewMapper.createMonth(empId);
    }

    //    各月份的创建客户数量统计
    public List<String> createMonthCount(Integer empId){
        return cuViewMapper.createMonthCount(empId);
    }

    //    查询当前登录员工的客户数量
    public Integer nums(Integer empId,Integer deptId,String startTime,String endTime){
        return cuViewMapper.nums(empId,deptId,1,1,startTime,endTime);
    }

    //  查询当前登录员工的客户类型比率
    public List<CuViewVo> type(Integer empId,Integer deptId,String startTime,String endTime){
        return cuViewMapper.type(empId,deptId,1,1,startTime,endTime);
    }

    //    客户分布
    public List<CuViewVo> china(Integer empId){
        List<CuViewVo> cu= cuViewMapper.china(empId,1);
        System.out.println("cu:"+cu);
        String str;
        int offset = -1;
        for(int i=0;i<cu.size();i++){
            if (cu.get(i).getValue()!=0) {
                if ((offset = cu.get(i).getName().indexOf("省")) != -1 || (offset = cu.get(i).getName().indexOf("市")) != -1 || (offset = cu.get(i).getName().indexOf("回族自治区")) != -1 || (offset = cu.get(i).getName().indexOf("维吾尔自治区")) != -1 || (offset = cu.get(i).getName().indexOf("自治区")) != -1) {
                    str = cu.get(i).getName().substring(0, offset);
                    System.out.println("匹配后：" + str);
                    cu.get(i).setName(str);
                }
            }
        }
        return cu;
    }

    //    查询下单数量
    public Map<String,List> selectOrderNum(Integer empId,Integer deptId,String startTime,String endTime){
        List<CuSelectOrderNum> orderNums = cuViewMapper.selectOrderNum(empId,deptId,startTime,endTime);
        List<String> name=new ArrayList<>();
        List<Integer> value=new ArrayList<>();
        orderNums.forEach(num->{
            name.add(num.getName());
            value.add(num.getValue());
        });
        Map<String,List> map=new HashMap<>();
        map.put("name",name);
        map.put("value",value);
        return map;
    }

}
